package com.test.service.impl;

import com.test.bindings.OutputMessageBinding;
import com.test.service.CollectionService;
import com.test.model.CollectionRequest;
import com.test.service.CollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @Description:
 * @Author: zrk
 * @Date: 2019/12/5
 */
@Service
@EnableBinding(OutputMessageBinding.class)
@Slf4j
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private OutputMessageBinding outputMessageBinding;
    private int index;

    private int count;

    private final String[] keys = {"orange", "black", "green"};
    /**
     * @param schoolName
     * @param content
     */
    @Override
    public void getCollection(String schoolName, String content) {


        /*StringBuilder builder = new StringBuilder("Hello to ");
        if (++this.index == 3) {
            this.index = 0;
        }
        String key = keys[this.index];
        builder.append(key).append(' ');
        builder.append(Integer.toString(++this.count));
        String payload = builder.toString();
        log.info(payload);

        // add kv pair - routingkeyexpression (which matches 'type') will then evaluate
        // and add the value as routing key
        Message<String> msg = new GenericMessage<>(payload, Collections.singletonMap("type", key));
        boolean send = outputMessageBinding.output().send(msg);
        log.info("mq发送消息结果=="+send);*/
        // return rest call

        CollectionRequest request = new CollectionRequest();
        request.setSchoolName(schoolName);
        request.setContent(content);
        //boolean version = outputMessageBinding.output().send(MessageBuilder.withPayload(request).setHeader("my-header", "你的header").build());

      boolean version = outputMessageBinding.output().send(MessageBuilder.withPayload(request).build());
        log.info("mq发送消息结果=="+version);
    }
}
